/*     */ package org.appfuse.util;
/*     */ 
/*     */ import java.text.ParseException;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Calendar;
/*     */ import java.util.Date;
/*     */ import java.util.GregorianCalendar;
/*     */ import java.util.Locale;
/*     */ import java.util.MissingResourceException;
/*     */ import java.util.ResourceBundle;
/*     */ import org.apache.commons.logging.Log;
/*     */ import org.apache.commons.logging.LogFactory;
/*     */ import org.springframework.context.i18n.LocaleContextHolder;
/*     */ 
/*     */ public final class DateUtil
/*     */ {
/*  25 */   private static Log log = LogFactory.getLog(DateUtil.class);
/*     */   private static final String TIME_PATTERN = "HH:mm";
/*     */ 
/*     */   public static String getDatePattern()
/*     */   {
/*  40 */     Locale locale = LocaleContextHolder.getLocale();
/*     */     String defaultDatePattern;
/*     */     try
/*     */     {
/*  43 */       defaultDatePattern = ResourceBundle.getBundle("ApplicationResources", locale).getString("date.format");
/*     */     }
/*     */     catch (MissingResourceException mse) {
/*  46 */       defaultDatePattern = "MM/dd/yyyy";
/*     */     }
/*     */ 
/*  49 */     return defaultDatePattern;
/*     */   }
/*     */ 
/*     */   public static String getDateTimePattern() {
/*  53 */     return getDatePattern() + " HH:mm:ss.S";
/*     */   }
/*     */ 
/*     */   public static String getDate(Date aDate)
/*     */   {
/*  65 */     String returnValue = "";
/*     */ 
/*  67 */     if (aDate != null) {
/*  68 */       SimpleDateFormat df = new SimpleDateFormat(getDatePattern());
/*  69 */       returnValue = df.format(aDate);
/*     */     }
/*     */ 
/*  72 */     return returnValue;
/*     */   }
/*     */ 
/*     */   public static Date convertStringToDate(String aMask, String strDate)
/*     */     throws ParseException
/*     */   {
/*  89 */     SimpleDateFormat df = new SimpleDateFormat(aMask);
/*     */ 
/*  91 */     if (log.isDebugEnabled())
/*  92 */       log.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");
/*     */     Date date;
/*     */     try {
/*  96 */       date = df.parse(strDate);
/*     */     }
/*     */     catch (ParseException pe) {
/*  99 */       throw new ParseException(pe.getMessage(), pe.getErrorOffset());
/*     */     }
/*     */ 
/* 102 */     return date;
/*     */   }
/*     */ 
/*     */   public static String getTimeNow(Date theTime)
/*     */   {
/* 113 */     return getDateTime("HH:mm", theTime);
/*     */   }
/*     */ 
/*     */   public static Calendar getToday()
/*     */     throws ParseException
/*     */   {
/* 123 */     Date today = new Date();
/* 124 */     SimpleDateFormat df = new SimpleDateFormat(getDatePattern());
/*     */ 
/* 128 */     String todayAsString = df.format(today);
/* 129 */     Calendar cal = new GregorianCalendar();
/* 130 */     cal.setTime(convertStringToDate(todayAsString));
/*     */ 
/* 132 */     return cal;
/*     */   }
/*     */ 
/*     */   public static String getDateTime(String aMask, Date aDate)
/*     */   {
/* 145 */     SimpleDateFormat df = null;
/* 146 */     String returnValue = "";
/*     */ 
/* 148 */     if (aDate == null) {
/* 149 */       log.warn("aDate is null!");
/*     */     } else {
/* 151 */       df = new SimpleDateFormat(aMask);
/* 152 */       returnValue = df.format(aDate);
/*     */     }
/*     */ 
/* 155 */     return returnValue;
/*     */   }
/*     */ 
/*     */   public static String convertDateToString(Date aDate)
/*     */   {
/* 167 */     return getDateTime(getDatePattern(), aDate);
/*     */   }
/*     */ 
/*     */   public static Date convertStringToDate(String strDate)
/*     */     throws ParseException
/*     */   {
/* 178 */     return convertStringToDate(getDatePattern(), strDate);
/*     */   }
/*     */ }

